package com.faxsun.web.endpoint.live;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import mtons.modules.pojos.Data;
import mtons.modules.pojos.Page;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.faxsun.core.persist.entity.LivePO;
import com.faxsun.core.pojos.enums.ChannelStatusType;
import com.faxsun.core.service.UserService;
import com.faxsun.core.service.WechatService;
import com.faxsun.web.endpoint.entity.ChannelJSON;

@RestController
@RequestMapping("/api/v1/live")
public class ChannelEndpoint {

	private static final Logger log = LoggerFactory.getLogger(ChannelEndpoint.class);

	@Autowired
	private WechatService channelService;
	@Autowired
	private UserService userService;

	/**
	 * 获取频道列表 url: <br>
	 * 1) http://www.faxsun.com/api/v1/live/channel/list<br>
	 * 2) http://www.faxsun.com/api/v1/live/channel/list?pn=2 <br>
	 * 3) http://www.faxsun.com/api/v1/live/channel/list?type=PAST <br>
	 * 
	 * Response: 如果type错误，http status为400
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param channelType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/channel/list", method = RequestMethod.GET)
	public Data listChannels(HttpServletRequest request,
			@RequestParam(required = false, value = "pn", defaultValue = "1") Integer pageNo,
			@RequestParam(required = false, value = "max", defaultValue = "8") Integer pageSize,
			@RequestParam(required = false, value = "type", defaultValue = "ALL") ChannelStatusType channelType) {

		Data ret = Data.failure("获取频道信息失败");
		try {
			Page page = new Page(pageSize);
			page.setPageNo(pageNo);

			switch (channelType) {
			case PAST:
				this.channelService.pagingPastChannels(page);
				break;
			case PRESENT:
				this.channelService.pagingPresentChannels(page);
				break;
			case FUTURE:
				this.channelService.pagingFutureChannels(page);
				break;
			case ALL:
				// 按开始时间降序排列
				page = this.channelService.pagingAllEvent(page, false);
				break;

			}

			try {
				List<LivePO> channels = (List<LivePO>) page.getResults();
				if (channelType.equals(ChannelStatusType.ALL)) {
					Collections.sort(channels, new Comparator<LivePO>() {

						@Override
						public int compare(LivePO o1, LivePO o2) {
							return o1.getStartTime().compareTo(o2.getStartTime());
						}

					});
				}
				List<ChannelJSON> channelsJSON = new ArrayList<ChannelJSON>();
				for (LivePO channel : channels) {
					channelsJSON.add(ChannelJSON.build(channel, request.getContextPath()));
				}
				page.setResults(channelsJSON);
			} catch (Exception e) {

			}

			ret = Data.success("获取频道成功！");
			ret.setData(page);
		} catch (Exception e) {
			log.error("", e);
		}
		return ret;
	}

	@RequestMapping(value = "/channel/info", method = RequestMethod.GET)
	public Data getChannelInfo(HttpServletRequest request,
			@RequestParam(required = true, value = "channelid", defaultValue = "0") Long id) {

		Data ret = Data.failure("获取频道信息失败");
		try {
			if (id > 0) {
				LivePO channel = this.channelService.findEventById(id);
				if (channel != null) {
					ret = Data.success("获取频道信息成功");
					ret.setData(channel);
				}
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return ret;
	}

}
